package cn.humaohua.study.yanmo_design_pattern.ch21;

public class Client {
    public static void main(String[] args) throws Exception {
        //准备上下文
        String file = "/Users/hmh/Documents/docs/source_code/git/yanmo_design_pattern/src/main/java/cn/humaohua/study/yanmo_design_pattern/ch21/conf.xml";
        test1(file);
        test2(file);
    }

    private static void test1(String file) throws Exception {
        Context c = new Context(file);

        //想要获取c元素的值，也就是如下表达式的值："root/a/b/c"
        ElementExpression root = new ElementExpression("root");
        ElementExpression aEle = new ElementExpression("a");
        ElementExpression bEle = new ElementExpression("b");
        ElementTerminalExpression cEle = new ElementTerminalExpression("c");

        //组合起来
        root.addEle(aEle);
        root.addEle(bEle);
        root.addEle(cEle);
        //调用
        String[] ss = root.interpret(c);
        System.out.println("c的值是:" + ss[0]);
    }

    private static void test2(String file) throws Exception {
        Context c = new Context(file);

        //想要获取c元素的值，也就是如下表达式的值："root/a/b/c"
        ElementExpression root = new ElementExpression("root");
        ElementExpression aEle = new ElementExpression("a");
        ElementExpression bEle = new ElementExpression("b");
        ElementExpression cEle = new ElementExpression("c");
        PropertyTerminalExpression propEle = new PropertyTerminalExpression("name");

        //组合起来
        root.addEle(aEle);
        root.addEle(bEle);
        root.addEle(cEle);
        root.addEle(propEle);
        //调用
        String[] ss = root.interpret(c);
        System.out.println("c的name属性值是:" + ss[0]);
    }
}
